package org.jeecg.common.util;

import cn.hutool.core.io.FileUtil;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {
    private static final String contentType = "application/json;charset=utf-8";
    private static final String charset = "UTF-8";
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectionPool(new ConnectionPool(500, 5, TimeUnit.MINUTES)).connectTimeout(10000, TimeUnit.MILLISECONDS)
            .readTimeout(60000, TimeUnit.MILLISECONDS).build();

    public static void downloadFile(String filePath, String url) {
        Request request = null;
        Response response = null;
        try{
            request = new Request.Builder().url(url).get().build();
            response = client.newCall(request).execute();
            if(200 != response.code()){
                String resBody = new String(response.body().bytes(), "UTF-8");
                System.out.println(resBody);
//            throw new Exception(response.message());
                throw new Exception(resBody);
            }
            FileUtil.writeBytes(response.body().bytes(),filePath);
        }catch (Exception e){

        }
    }


    public static void main1(String[] args) {
//        SELECT idcard_img , idcard_img_back from hy_driver where del_flag=0 and idcard_img is not null
        //168
        String url ="https://www.hebeiyiliuba.com/wlhy168/sys/common/static/";
        //前
        String basePath = "D:\\idcard\\168\\idcard_img\\";
        List<String> idcard_imgs = FileUtil.readLines("D:\\idcard\\168\\idcard_img.txt", "UTF-8");
        //后
//        String basePath = "D:\\idcard\\idcard_img2\\";
//        List<String> idcard_imgs = FileUtil.readLines("D:\\idcard\\idcard_img2.txt", "UTF-8");
        int count = 0;
        for (String idcardImg : idcard_imgs) {
            String substring = idcardImg.substring(idcardImg.lastIndexOf('.'));
            String filePath = basePath + count + substring;
            downloadFile(filePath,url + idcardImg);
            System.out.println(count + "下载完成");
            count++;
        }
    }

    public static void main(String[] args) {
//        SELECT idcard_img , idcard_img_back from hy_driver where del_flag=0 and idcard_img is not null
        //广福源 身份证下载
        String url ="https://www.sxgfy666.com/wlhy168/sys/common/static/";
        //前
        String basePath = "D:\\idcard\\gfy\\idcard_img\\0\\";
        List<String> idcard_imgs = FileUtil.readLines("D:\\idcard\\gfy\\idcard_img\\idcard_img.txt", "UTF-8");
        //后
//        String basePath = "D:\\idcard\\gfy\\idcard_img\\back";
//        List<String> idcard_imgs = FileUtil.readLines("D:\idcard\gfy\idcard_img\idcard_img_back.txt", "UTF-8");
        int count = 0;
        for (String idcardImg : idcard_imgs) {
            String substring = idcardImg.substring(idcardImg.lastIndexOf('.'));
            String filePath = basePath + count + substring;
            downloadFile(filePath,url + idcardImg);
            System.out.println(count + "下载完成");
            count++;
        }
    }


    //lable文件创建
    public static void main2(String[] args) throws IOException {

//        File lable_file = new File("D:\\idcard\\idcard_label.txt");
//        List<String> c = new ArrayList<>();
//
//        File baseDir = new File("D:\\idcard\\idcard_img\\");
//        File[] files=baseDir.listFiles();
//        for (File file : files) {
//            String fileName = file.getName();
//            c.add("idcard_img/" + fileName + " 0");
//        }
//
//        File baseDir2 = new File("D:\\idcard\\idcard_img2\\");
//        File[] files2=baseDir2.listFiles();
//
//        for (File file : files2) {
//            String fileName = file.getName();
//            c.add("idcard_img2/" + fileName + " 1");
//        }
//
//
//        FileUtil.writeLines(c,lable_file , "utf-8");



    }
}
